package com.creditease.mobile.news.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.creditease.mobile.news.model.ImgModel;
import com.creditease.mobile.news.model.NewsModel;
import com.creditease.mobile.vo.NewsDetail;

import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    public static void deleteDataToNewsListTabel(
            SQLiteDatabase paramSQLiteDatabase, String paramString) {
        paramSQLiteDatabase.delete("news_list", "news_col_id='" + paramString
                + "'", null);
    }

    public static Cursor getDataFromNewsListTable(Context paramContext,
                                                  SQLiteDatabase paramSQLiteDatabase) {
        String[] arrayOfString = new String[1];
        arrayOfString[0] = "news_auto_id AS _id,news_col_id,news_doc_id,news_img_src,news_digest,news_reply_count,news_url,news_title,news_ptime,news_is_hasimg";
        return paramSQLiteDatabase.query("news_list", arrayOfString, null,
                null, null, null, null, null);
    }

    public static ArrayList<String> getSpecialArticleIds(
            SQLiteDatabase paramSQLiteDatabase, String paramString) {
        String[] arrayOfString = new String[1];
        arrayOfString[0] = "news_doc_id";
        Cursor localCursor = paramSQLiteDatabase.query("news_list",
                arrayOfString, "news_col_id='" + paramString + "'", null, null,
                null,
                "news_index ASC ,news_auto_id ASC ,news_special_tname DESC ",
                null);
        ArrayList localArrayList = new ArrayList();
        localCursor.moveToFirst();
        while (!localCursor.isAfterLast()) {
            localArrayList.add(localCursor.getString(localCursor
                    .getColumnIndex("news_doc_id")));
            localCursor.moveToNext();
        }
        localCursor.close();
        return localArrayList;
    }

    public static void insertDataToCityTable(
            SQLiteDatabase paramSQLiteDatabase, String paramString1,
            String paramString2, String paramString3) {
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("city_name", paramString1);
        localContentValues.put("city_province", paramString2);
        localContentValues.put("city_pinyin", paramString3);
        localContentValues.put("city_select_count", Integer.valueOf(0));
        paramSQLiteDatabase.insert("city_list", null, localContentValues);
    }

    public static void insertDataToCommentsListTable(
            SQLiteDatabase paramSQLiteDatabase, String paramString1,
            String paramString2, String paramString3, String paramString4,
            String paramString5, String paramString6, String paramString7,
            String paramString8, String paramString9, String paramString10,
            String paramString11) {
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("comments_col_id", paramString1);
        localContentValues.put("comments_doc_id", paramString2);
        localContentValues.put("comments_reply_count", paramString3);
        localContentValues.put("comments_title", paramString4);
        localContentValues.put("comments_tie", paramString5);
        localContentValues.put("comments_author", paramString6);
        localContentValues.put("comments_reply_board", paramString7);
        localContentValues.put("comments_postid", paramString8);
        localContentValues.put("comments_floor", paramString9);
        localContentValues.put("comments_is_read", paramString11);
        localContentValues.put("comments_ptime", paramString10);
        paramSQLiteDatabase.insert("comments_list", null, localContentValues);
    }

    public synchronized static List<NewsModel> getDataFromNewsListTable(NewsSQLiteOpenHelper dbHelper,
                                                                        String cid, int offset, int max) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        List<NewsModel> result = new ArrayList<NewsModel>();
        String sql = "select * from news_list where news_col_id=? limit ?,?";
        Cursor cursor = null;
        try {
            cursor = db.rawQuery(sql, new String[]{cid, String.valueOf(offset), String.valueOf(max)});
            while (cursor.moveToNext()) {

                String id = cursor.getString(0);
                // String cid = cursor.getString(1);
                String docid = cursor.getString(2);
                String imgsrc = cursor.getString(3);
                String digest = cursor.getString(4);
                String replyCount = cursor.getString(5);
                String url = cursor.getString(6);
                String title = cursor.getString(7);
                String ptime = cursor.getString(8);
                String subtitle = cursor.getString(9);

                NewsModel model = new NewsModel(id, cid, "", replyCount, "", digest, url, docid, title, "", "", "", subtitle,
                        imgsrc, ptime);

                result.add(model);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }
        return result;
    }

    public static NewsDetail getDataFromNewsTable(NewsSQLiteOpenHelper dbHelper,
                                                  String docId) {

        String sql = "select news_auto_id,news_doc_id,title, ptime,body, hasNext, source from news where news_doc_id=? ";
        Cursor cursor = null;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            cursor = db.rawQuery(sql, new String[]{docId});
            if (cursor.moveToNext()) {
                String id = cursor.getString(0);
                String docid = cursor.getString(1);
                String title = cursor.getString(2);
                String ptime = cursor.getString(3);
                String body = cursor.getString(4);
                String hasNext = cursor.getString(5);
                String source = cursor.getString(6);

                NewsDetail model = new NewsDetail(id, docid, title, ptime, body, hasNext, source);
                return model;
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
           db.close();
        }
        return null;
    }

    public static List<NewsDetail> getDatasFromNewsTable(SQLiteDatabase db,
                                                         String colId) {

        String sql = "select news_auto_id,news_doc_id,title, ptime,body, hasNext, source from news where news_col_id=? ";
        Cursor cursor = null;

        cursor = db.rawQuery(sql, new String[]{colId});
        List<NewsDetail> list = new ArrayList<NewsDetail>();
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            String docid = cursor.getString(1);
            String title = cursor.getString(2);
            String ptime = cursor.getString(3);
            String body = cursor.getString(4);
            String hasNext = cursor.getString(5);
            String source = cursor.getString(6);

            NewsDetail model = new NewsDetail(id, docid, title, ptime, body, hasNext, source);
            list.add(model);
        }
        db.close();

        return list;
    }

    public static void insertDataToPhotoListTable(
            SQLiteDatabase paramSQLiteDatabase, String paramString1,
            String paramString2, String paramString3, String paramString4,
            String paramString5, boolean paramBoolean) {
        if (paramBoolean) {
            String[] arrayOfString = new String[1];
            arrayOfString[0] = paramString1;
            Cursor localCursor = paramSQLiteDatabase.rawQuery(
                    "select * from photo_list where photo_setid=? ",
                    arrayOfString);
            int i = 0;
            if (localCursor != null)
                i = localCursor.getCount();
            localCursor.close();
            if (i == 0) {
                ContentValues localContentValues2 = new ContentValues();
                localContentValues2.put("photo_setid",
                        new Integer(paramString1));
                localContentValues2.put("photo_setname", paramString2);
                localContentValues2.put("photo_clientcover", paramString3);
                localContentValues2.put("photo_imgsum", paramString4);
                localContentValues2.put("photo_date", paramString5);
                paramSQLiteDatabase.insert("photo_list", null,
                        localContentValues2);
            }
        }
        while (true) {
            // return;
            ContentValues localContentValues1 = new ContentValues();
            localContentValues1.put("photo_setid", new Integer(paramString1));
            localContentValues1.put("photo_setname", paramString2);
            localContentValues1.put("photo_clientcover", paramString3);
            localContentValues1.put("photo_imgsum", paramString4);
            localContentValues1.put("photo_date", paramString5);
            paramSQLiteDatabase.insert("photo_list", null, localContentValues1);
        }
    }

    public static void insertDataToPicTable(SQLiteDatabase paramSQLiteDatabase,
                                            String paramString1, String paramString2) {
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("pic_setid", paramString1);
        localContentValues.put("pic_json", paramString2);
        paramSQLiteDatabase.insert("pic", null, localContentValues);
    }

    public static void insertDataToReadStatusTable(
            SQLiteDatabase paramSQLiteDatabase, String paramString1,
            String paramString2, String paramString3) {
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("article_read_doc_id", paramString1);
        localContentValues.put("article_read_col_id", paramString2);
        localContentValues.put("article_read_is_read", paramString3);
        paramSQLiteDatabase.insert("article_read_status_list", null,
                localContentValues);
    }

    public static boolean isSpecial(SQLiteDatabase paramSQLiteDatabase,
                                    String paramString1, String paramString2) {
        int i = 0;
        String[] arrayOfString = new String[1];
        arrayOfString[0] = "news_special_id";
        Cursor localCursor = paramSQLiteDatabase.query("news_list",
                arrayOfString, "news_doc_id='" + paramString1 + "' and "
                + "news_col_id" + "='" + paramString2 + "'", null,
                null, null, null, null);
        if (localCursor.moveToFirst()) {
            String str = localCursor.getString(localCursor
                    .getColumnIndex("news_special_id"));
            if ((str != null) && (!"".equals(str)))
                i = 1;
        }
        localCursor.close();
        return true;
    }

    public static List<String> getDataFromImgTable(SQLiteDatabase db,
                                                   String docid) {

        List<String> result = new ArrayList<String>();
        String sql = "select url from img where news_doc_id=? ";
        Cursor cursor = null;
        try {
            cursor = db.rawQuery(sql, new String[]{docid});
            while (cursor.moveToNext()) {

                String url = cursor.getString(0);

                result.add(url);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }
        return result;
    }


}